
import './PromiseRef';
import './prototype';
import Http from "./request";
import Save from './save';

(function (w) {
    const $ = new Http();
    $.copy = function (obj) {
        return JSON.parse(JSON.stringify(obj));
    }
    $.params = function (url) {
        if (!url) {
            url = document.location.search;
        }
        var arrObj = url.split("?");
        if (arrObj.length > 1) {
            var arrPara = url.substr(url.indexOf('?') + 1).split("&");
            var arr;
            var list = {};
            for (var i = 0; i < arrPara.length; i++) {
                arr = arrPara[i].split("=");
                if (arr) {
                    list[arr[0]] = $.decode(arr[1] || "");
                }
            }
            return list;
        } else {
            return {};
        }
    }

    $.parseUrl = function (params) {
        var parts = [];
        $.forEach(params, function serialize(val, key) {
            if (val === null || typeof val === 'undefined') {
                return;
            }
            if ($.isArray(val)) {
                key = key + '[]';
            }
            if (!$.isArray(val)) {
                val = [val];
            }
            $.forEach(val, function parseValue(v) {
                if ($.isDate(v)) {
                    v = v.toISOString();
                } else if ($.isObject(v)) {
                    v = JSON.stringify(v);
                }
                parts.push($.encode(key) + '=' + $.encode(v));
            });
        });
        return parts.join('&');
    }

    $.encode = function (val) {
        return encodeURIComponent(val).
            replace(/%40/gi, '@').
            replace(/%3A/gi, ':').
            replace(/%24/g, '$').
            replace(/%2C/gi, ',').
            replace(/%20/g, '+').
            replace(/%5B/gi, '[').
            replace(/%5D/gi, ']');
    }
    $.isArray = function (val) {
        return toString.call(val) === '[object Array]';
    }

    $.forEach = function (obj, fn) {
        if (obj === null || typeof obj === 'undefined') {
            return;
        }
        if (typeof obj !== 'object' && !$.isArray(obj)) {
            obj = [obj];
        }
        if ($.isArray(obj)) {
            for (var i = 0, l = obj.length; i < l; i++) {
                fn.call(null, obj[i], i, obj);
            }
        } else {
            for (var key in obj) {
                if (Object.prototype.hasOwnProperty.call(obj, key)) {
                    fn.call(null, obj[key], key, obj);
                }
            }
        }
    }
    $.isFunction = function (value) {
        return value && typeof (value) === "function";
    };
    $.isObject = function (val) {
        return val && val.toString() == '[object Date]';
    }

    $.isDate = function (val) {
        return val && val.toString() == '[object Date]';
    }
    $.decode = function (val) {
        return decodeURIComponent(val);
    }
    $.toBase64 = function (file) {
        return new Promise((res) => {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function (e) {
                res(reader.result);
            };
        })
    }
    $.cache = new Save('default_cache')
    w.$ = $;
}(window));
